import { getToken } from '@inbiz/utils';
import { request } from '@inbiz/utils';
import { InbizComment } from './typings';

export const GetDocCommentList = (params: {
  pageNum: number;
  pageSize: number;
  docId: number;
  docType?: 1 | 2;
}): Promise<{
  result: number;
  data: {
    CommentList: InbizComment[];
    TotalCommentCount: number;
  };
  count: number;
  message: string;
  dataDescription: string;
}> => {
  return request(`/api/services/Doc/GetDocComment`, {
    params: {
      token: getToken(),
      pageNum: params.pageNum,
      pageSize: params.pageSize,
      docId: params.docId,
      entryType: params.docType || 2,
      r: new Date().getTime(),
    },
  });
};

export const AddOrUpdateComment = (params: {
  comment: string;
  docId: number;
  annotationId?: number;
  docType?: 1 | 2;
}): Promise<{
  result: number;
}> => {
  if (params.annotationId) {
    return request(`/WebCore`, {
      method: 'POST',
      requestType: 'form',
      data: {
        module: 'WebClient',
        fun: 'AddFileCommentReply',
        entryType: params.docType || 2,
        entryId: params.docId,
        commentParentId: params.annotationId,
        commentContent: escape(params.comment),
        anonymous: false,
      },
    });
  }
  return request(`/api/services/Doc/AddDocComment`, {
    method: 'POST',
    data: {
      CommentContent: escape(params.comment),
      DocId: params.docId,
      EntryType: params.docType || 2,
      Token: getToken(),
      isAnonymous: false,
    },
  });
};

export const DeleteComment = (params: {
  commentId: number;
  docType?: 1 | 2;
}): Promise<{
  result: number;
}> => {
  return request(`/WebCore`, {
    method: 'POST',
    requestType: 'form',
    data: {
      module: 'WebClient',
      fun: 'DelFileComment',
      commentId: params.commentId,
      entryType: params.docType || 2,
    },
  });
};
